<?php
/**
 * @file
 * Controller represent the output of a file in "/Controller".
 *
 * @category   rpd
 * @package    Controller
 * @subpackage Controller
 * @copyright  Copyright (c) 2010 Rpd-Team (http://code.google.com/p/rpd)
 * @license    BSD License
 * @link       http://code.google.com/p/rpd/wiki/Controller
 * @since      0.0.1
 * @version    0.0.1
 */

/**
 * This class represent the output of a given controller.
 *
 * If you want to have the output of any controller just generate a new class
 * with the path and name like this:
 *
 * @code
 *   // get what happens when client calls 'path/name'
 *   $c = new Controller('path/name');
 * @endcode
 *
 * It will run and contain the output like a string:
 *
 * @code
 *   // show me the content
 *   echo $c;
 * @endcode
 *
 * @author Pretzlaw
 */

class rpd_Controller
{
	/**
	 * Stores the specified controller file
	 * @var String
	 */
	private $_file = "";

	/**
	 * Stores the content of the controller
	 * @var String
	 */
	private $_content = NULL;

	/**
	 * Calls the given controller file and generate the output
	 * @return String Output
	 */
	public function __toString()
	{
		if ($this->_content === NULL)
		{
			if (file_exists($this->_file))
			{
				ob_start();
				include($this->_file);
				$this->_content = ob_get_clean();
			}
			else
			{
				$this->_content = "";
			}
		}
		return $this->_content;
	}

	/**
	 *
	 * @param String $file Controllername (e.g. "Path/Name") without ".php"
	 * @return void
	 */
	public function __construct($file)
	{
		$this->_file = 'Controller/' . $file . '.php';
	}
}

if (!isset(Hook::$classes["Controller"]))
{
	class Controller extends rpd_Controller {}
}